package com.hjz.hongbaotest.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.mapapi.search.geocode.GeoCoder;
import com.hjz.hongbaotest.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * ************************************************************
 * 类描述：
 * 作者：Administrator
 * 创建时间：2017-09-30 12:43
 * <p>
 * ************************************************************
 */

public class HandRedActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_red_name,et_num,et_money,et_password;
//    private TextView tv_address;
    private Button btn_sure;
    private ImageView back_img;
    private double latitude;
    private double longitude;
    private GeoCoder mSearch = null; // 搜索模块
    private String address=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hand_red_layout);

        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        latitude=intent.getDoubleExtra("clickLatitude",0);
        longitude=intent.getDoubleExtra("clickLongitude",0);

        et_red_name= (EditText) findViewById(R.id.et_red_name);
        et_num= (EditText) findViewById(R.id.et_num);
        et_money= (EditText) findViewById(R.id.et_money);
        et_password= (EditText) findViewById(R.id.et_password);
//        tv_address= (TextView) findViewById(R.id.tv_address);
        btn_sure= (Button) findViewById(R.id.btn_sure);
        back_img= (ImageView) findViewById(R.id.back_img);
        back_img.setOnClickListener(this);
        btn_sure.setOnClickListener(this);
//        tv_address.setText(address);

    }


    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.btn_sure){
                handRed();
        }else if(v.getId()==R.id.back_img){
            finish();
        }
    }

    private void handRed() {
        SharedPreferences share = getSharedPreferences("test", MODE_PRIVATE);
        String sessionId = share.getString("sessionId", "");

        String redTitle=et_red_name.getText().toString().trim();
        String redNum=et_money.getText().toString().trim();
        String num=et_num.getText().toString().trim();
        String myTwoPassword=et_password.getText().toString().trim();

        OkGo.<String>post("http://192.168.1.133:8080/redmap/member/red/appFa.shtml")
                .params("sessionId",sessionId)
                .params("longitude",longitude)
                .params("latitude",latitude)
                .params("redTitle",redTitle)
                .params("redNum",redNum)
                .params("num",num)
               .params("myTwoPassword",myTwoPassword)
               .execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                try {
                    JSONObject obj = new JSONObject(response.body().toString());
                    String msg = obj.getString("msg");
                    String success=obj.getString("success");
                    if (success.equals("true")){
                        startActivity(new Intent(HandRedActivity.this,MainActivity.class));
                        finish();
                    }
                    Toast.makeText(HandRedActivity.this,msg,Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mSearch.destroy();
    }
}
